﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudyCode.Generics.GenericsClass
{
    public class Repository<TEntity> where TEntity : Entity, new() 
    {
        List<TEntity> _entityList = new List<TEntity>(); 
        public void Create(TEntity entity)
        {
            _entityList.Add(entity);
        }

        public void AddRange(IList<TEntity> entityList)
        {
            _entityList.AddRange(entityList);
        }

        public void Update(TEntity entity)
        {
            var oldItem=_entityList.FirstOrDefault(item => item.Id == entity.Id);
            _entityList.Remove(oldItem);
            _entityList.Add(entity);
        }

        public void Remove(TEntity entity)
        {
            _entityList.Remove(_entityList.FirstOrDefault(item => item.Id == entity.Id));
        }

        public List<TEntity> GetAll()
        {
            return _entityList;
        }
    }

    public abstract class Entity
    {
        public long Id { get; set; }
    }


    public class Student:Entity
    {
        public string Name { get; set; }
        public string StudyIdentify { get; set; }
    }

    public class Teacher : Entity
    {
        public string Name { get; set; }
        public string WorkIdentify { get; set; }
    }
}
